package com.blueheart.easyexcel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.read.listener.ReadListener;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Slf4j
public class CaseTemplateBOReadListener implements ReadListener<CaseTemplatesBO> {
    private int subFlag = 0x01;

    private final List<TaxCaseAnalysisBO> taxCaseAnalysisBOS;

    private TaxCaseAnalysisBO taxCaseAnalysisBO;

    private List<TaxDescriptionBO> caseRegulationDescList = new LinkedList<>();

    private List<TaxDescriptionBO> caseAnalysisDescList = new LinkedList<>();

    private static final Gson gson = new Gson();

    public CaseTemplateBOReadListener(final LinkedList<TaxCaseAnalysisBO> taxCaseAnalysisBOS) {
        this.taxCaseAnalysisBOS = taxCaseAnalysisBOS;
    }

    @Override public void onException(final Exception exception, final AnalysisContext context) throws Exception {
        log.error("发生错误 context:{}",context,exception);
    }

    @Override public void invokeHead(final Map<Integer, ReadCellData<?>> headMap, final AnalysisContext context) {

    }


    @Override public boolean hasNext(final AnalysisContext context) {
        return true;
    }

    @Override public void invoke(final CaseTemplatesBO caseTemplatesBO, final AnalysisContext analysisContext) {

        if (Objects.isNull(taxCaseAnalysisBO)) {
            taxCaseAnalysisBO = new TaxCaseAnalysisBO();
        }
        switch (caseTemplatesBO.getLawName()) {
        case CaseTemplatesBO.CASE_TITLE:
            taxCaseAnalysisBO.setTitle(caseTemplatesBO.getLawValue());
            break;
        case CaseTemplatesBO.CASE_KEYWORD:
            taxCaseAnalysisBO.setKeyword(caseTemplatesBO.getLawValue());
            break;
        case CaseTemplatesBO.CASE_DESC:
            taxCaseAnalysisBO.setDescription(caseTemplatesBO.getLawValue());
            break;
        case CaseTemplatesBO.CASE_REGULATION:
        case CaseTemplatesBO.CASE_ANALYSIS:
            //设置
            subFlag = subFlag << 1;
            break;
        default:
            TaxDescriptionBO taxDescriptionBO = new TaxDescriptionBO();
            taxDescriptionBO.setType(caseTemplatesBO.getLawName());
            taxDescriptionBO.setInfo(caseTemplatesBO.getLawValue());
            if(subFlag == 0b10){
                caseRegulationDescList.add(taxDescriptionBO);
            }else if(subFlag == 0b100){
                caseAnalysisDescList.add(taxDescriptionBO);
            }
        }
    }

    @Override public void doAfterAllAnalysed(final AnalysisContext analysisContext) {
        //JSON
        taxCaseAnalysisBO.setRegulation(gson.toJson(caseRegulationDescList));
        taxCaseAnalysisBO.setAnalysis(gson.toJson(caseAnalysisDescList));
        //加到队列
        taxCaseAnalysisBOS.add(taxCaseAnalysisBO);

        //初始化下张表格参数、对象
        caseRegulationDescList.clear();
        caseAnalysisDescList.clear();
        subFlag = 0x01;
        taxCaseAnalysisBO = new TaxCaseAnalysisBO();
    }

}
